const inspirecloud = require('@byteinspire/api');
const R = require('../result/r');
let userService = require('../services/userService');
let JWTUtil = require('../util/jwtUtil');
class UserController {
    /**
     * 发送手机短信
     */
    async sendPhoneCode(ctx) {
        const {phone} = ctx.request.body
        let res = await inspirecloud.user.sendSMS(
            ctx,
            phone
        );
        ctx.body = R.ok("发送成功").data(res);
    }

    /**
     * 手机验证码登录
     */
    async loginByPhoneCode(ctx) {
        //console.log(ctx)
        // 从请求参数中获取电话号和验证码
        const {phone, code} = ctx.request.body
        try {
            const {userInfo} = await inspirecloud.user.loginByPhone(
                ctx,
                phone,
                code
            );
            ctx.body = R.ok("登录成功").data(userInfo);
        } catch (error) {
            ctx.body = R.error(error.message);
        }
    }

    async listAll(ctx) {
        let listAll = await userService.listAll();
        ctx.body = R.ok("查询成功").data(listAll);
    }

    async registerUser(ctx) {
        //console.log('ctx', ctx);
        const {username, password} = ctx.request.body;
        try {
            const userInfo = await inspirecloud.user.register(
                ctx,
                username,
                password
            );
            ctx.body = R.ok().data({
                userInfo
            })
        } catch (e) {
            ctx.body = R.error(e.message)
        }
    }

    async userInfo(ctx) {
        //console.log(ctx)
        ctx.body = R.ok().data(inspirecloud.user.current(ctx));
    }

    // 账号密码登陆
    async loginUser(ctx) {
        //console.log(ctx);
        // 从 params 中获取账号密码等参数
        const {username, password} = ctx.request.body;
        let userInfo;
        try {
            userInfo = await inspirecloud.user.login(
                ctx, // 注意，调用所有 inspirecloud.user 相关接口时，都需要传入云函数中的 context
                username,
                password
            );
            //生成token签名
            let payload = {
                'x-tt-session-v2': ctx.request.headers['x-tt-session-v2'],
                user_id: userInfo._id
            }
            let authorization =await JWTUtil.createToken(payload);
            ctx.body = R.ok().data({
                userInfo,
                authorization
            })
        } catch (e) {
            ctx.body = R.error(e.message)
        }
    };

    // 退出登陆
    async logoutUser(ctx) {
        //console.log(ctx);

        try {
            const userInfo = await inspirecloud.user.logout(
                ctx, // 注意，调用所有 inspirecloud.user 相关接口时，都需要传入云函数中的 context
            );

            ctx.body = R.ok().data({
                userInfo
            })
        } catch (e) {
            ctx.body = R.error(e.message)
        }
    };

    async updatePassword(ctx) {
        const {newPassword} = ctx.request.body;
        try {
            await inspirecloud.user.changePassword(
                ctx,
                newPassword
            );
            ctx.body = R.ok("修改成功")
        } catch (e) {
            ctx.body = R.error("修改失败")
        }
    };
}

module.exports = new UserController();